<?php
namespace app\home\Controller;
use think\Controller;
use think\Request;
use think\Db;
// use app\index\model\index;
class Index extends Controller
{
	// 首页
    public function index()
    {   
        header("content-type:text/html;charset=utf-8");

        // 今日发现
        $sql = "select * from man_goods order by g_addtime desc limit 20";
        $goodsDataFx = Db::query($sql);
        // 精选服饰
        $sql = "select * from man_goods where g_is_hot=1 and g_is_del=1 order by g_addtime asc limit 8";
        $goodsDataRm = Db::query($sql);
        // 轮播图
        $sql = "select * from man_brand limit 5";
        $adverdata = Db::query($sql);
        $typeData = $this->alltype();

        return view('index',[
            'typeData'=>$typeData,
            'goodsDataFx'=>$goodsDataFx,
            'goodsDataRm'=>$goodsDataRm,
            'adverdata'=>$adverdata
            ]
        );
    }

    // 精选品牌
    public function refined()
    {
        $typeData = $this->alltype();
        return view('refined',['typeData'=>$typeData]);
    }

    // 全球购
    public function go()
    {
        $typeData = $this->alltype();
        return view('go',['typeData'=>$typeData]);
    }

    // 品牌馆
    public function brand()
    {
        $typeData = $this->alltype();
        $request = Request::instance();
        $page = $request->get('page',1);
        $pagesize = 7;
        $offest = ($page-1)*$pagesize;
        $res = Db::query("select count(*) as num from man_brand");
        $lastpage = ceil($res[0]['num']/$pagesize);
        $uppage = $page -  1 < 1 ? 1 :$page-1;
        $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
        $page = array(
            'page'=>$page,
            'uppage'=>$uppage,
            'downpage'=>$downpage,
            'lastpage'=>$lastpage
        );
        $sql = "select * from man_brand limit $offest,$pagesize";
        $data = Db::query($sql);
        return view('brand',['branddata'=>$data,'page'=>$page]);
    }
    public function brandshoplist(){
        $b_id = input('get.b_id');
        $request = Request::instance();
        $page = $request->get('page',1);
        $pagesize = 21;
        $offest = ($page-1)*$pagesize;
        $res = Db::query("select count(*) as num from man_goods where b_id in($b_id)");
        $lastpage = ceil($res[0]['num']/$pagesize);
        $uppage = $page -  1 < 1 ? 1 :$page-1;
        $downpage = $page + 1> $lastpage ? $lastpage : $page+1;
        $page = array(
          'page'=>$page,
          'uppage'=>$uppage,
          'downpage'=>$downpage,
          'lastpage'=>$lastpage
        );
        $sql = "select * from man_goods where b_id in($b_id) limit $offest,$pagesize";
        $goodsdata = Db::query($sql);
        return view('brandshoplist',['goodsdata'=>$goodsdata,'page'=>$page,'b_id'=>$b_id]);
    }
    // 海淘专场
    public function gou_2()
    {
        $typeData = $this->alltype();
        return view('gou_2',['typeData'=>$typeData]);
    }

    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }
    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }
}
